package com.myblog.utils;

import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

public class FileUtilToLocal {
    public static String uploadToLocal(MultipartFile file) throws FileNotFoundException{
        // 获取文件原本的名字
        String originName = file.getOriginalFilename();
        // 判断文件是否是pdf文件
        Set<String> set = new HashSet<>();
        set.add(".pdf");
        set.add(".doc");
        set.add(".docx");
        set.add(".png");
        set.add(".jpg");
        set.add(".jpeg");
        set.add(".gif");
        // 取出文件的后缀
        int count = 0;
        for(int i = 0; i < originName.length(); i++){
            if(originName.charAt(i) == '.'){
                count = i;
                break;
            }
        }
        String endName = originName.substring(count); //取出文件类型
        String fileType = originName.substring(count + 1); //文件类型
        if(!set.contains(endName)){
            return new String("fileTypeError");
        }
        // 创建保存路径
        //日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String format = sdf.format(new Date());
        String path = ResourceUtils.getURL("classpath:").getPath()+"static/";
        String staticPath = path.replace('/', '\\').substring(1,path.length());
//        staticPath.replaceAll("/","\\");
        String savePath =
                staticPath + "images" ;
//                System.getProperty("user.dir") + "\\" +
//                        "files";
//                        + "\\" + fileType + "\\" + format;
        // 保存文件的文件夹
        File folder = new File(savePath);
        // 判断路径是否存在,不存在则自动创建
        if(!folder.exists()){
            folder.mkdirs();
        }
        String saveName = originName;
        try {
            file.transferTo(new File(folder,saveName));
//            String filePath = savePath + "/" + saveName;
            String filePath = savePath + "\\" + saveName;
            return new String(filePath);
        } catch (IOException e){
            return new String(e.getMessage());
        }
    }

    public boolean deleteFiles(String pathName){
        boolean flag = false;
        try {
            java.io.File myDelFile = new java.io.File(pathName.toString());
            myDelFile.delete();
            System.gc();
        }catch (Exception e) {
            System.out.println("删除文件操作出错");
            e.printStackTrace();
        }
        //删除失败时，返回false
        return flag;
    }

    public String disposeUserImgUrl(String url){
        String useAvatarImgUrl = null;
        if(url.length()>20) {
            int begin = url.indexOf("images");
            useAvatarImgUrl = url.substring(begin - 1);
        }
        return useAvatarImgUrl;
    }
}
